﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class LevelConsumChartController : BaseController<usersorder>
    {
        //
        // GET: /LevelConsumChart/

        public override BLL.BaseBll<usersorder> bll
        {
            get
            {
                return new UsersOrderBll();
            }

        }

        StoreBll storeBll = new StoreBll();

        public class OrderModel
        {
            public string levelName;
            public string rate;

        }

        public class OrderModel2
        {

            public string name;
            public int value;

        }
        //string date1, string date2,   string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) && 
        public ActionResult GetOrderAll(string date1, string date2, string storeid)
        {
            List<OrderModel> dataList = new List<OrderModel>();

            List<OrderModel2> dataList2 = new List<OrderModel2>();
            Expression<Func<usersorder, bool>> condition1 = null;
            Expression<Func<usersorder, bool>> condition2 = null;
            Expression<Func<usersorder, bool>> condition3 = null;
            Expression<Func<usersorder, bool>> condition4 = null;
            Expression<Func<usersorder, bool>> condition5 = null;
            Expression<Func<usersorder, bool>> condition6 = null;

            //  默认查询所有的 订单信息   默认是万达店的
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) && string.IsNullOrEmpty(storeid))
            {               
                // 1.
                var allCount = bll.Search(x => x.orderStatus == 1 ).ToList();//该店 所有的订单数量

                //2.  1 元一下
                condition1 = x => x.orderStatus == 1 && x.orderSumprice + x.discountMoney <= 1 ;
                var query1 = bll.Search(condition1).ToList();
                OrderModel2 orderModel1 = new OrderModel2();
                int a = query1.Count();
               double b = allCount.Count();
               
                //orderModel1.value = a;//(a / b).ToString("0.00%");
               orderModel1.name = "1元以下" + "     " + (a / b).ToString("0.00%");
                dataList2.Add(orderModel1);

                //3.  1--6元
                condition2 = x => x.orderStatus == 1 && x.orderSumprice + x.discountMoney > 1 && x.orderSumprice + x.discountMoney <= 6 ;
                var query2 = bll.Search(condition2).ToList();
                OrderModel2 orderModel2 = new OrderModel2();
                int a2 = query2.Count();
                double b2 = allCount.Count();               
                orderModel2.value = a2;//(a / b).ToString("0.00%");
                orderModel2.name = "1至6元" + "     " + (a2 / b2).ToString("0.00%");
                dataList2.Add(orderModel2);

                //3.  6-12 元
                condition3 = x => x.orderStatus == 1 && x.orderSumprice + x.discountMoney > 6 && x.orderSumprice + x.discountMoney <= 12 ;
                var query3 = bll.Search(condition3).ToList();

                OrderModel2 orderModel3 = new OrderModel2();
                int a3 = query3.Count();
                double b3 = allCount.Count();
               
                orderModel3.value = a3;
                orderModel3.name = "6至12元" + "     " + (a3 / b3).ToString("0.00%");
                dataList2.Add(orderModel3);

                //4.  12-18 元
                condition4= x => x.orderStatus == 1 && x.orderSumprice + x.discountMoney > 12 && x.orderSumprice + x.discountMoney <= 18 ;
                var query4 = bll.Search(condition4).ToList();

                OrderModel2 orderModel4 = new OrderModel2();
                int a4 = query4.Count();
                double b4 = allCount.Count();

                orderModel4.value = a4;
                orderModel4.name = "12至18元" + "     " + (a4 / b4).ToString("0.00%");
                dataList2.Add(orderModel4);

                //4.  18-25 元
                condition5 = x => x.orderStatus == 1 && x.orderSumprice + x.discountMoney > 18 && x.orderSumprice + x.discountMoney <= 25 ;
                var query5 = bll.Search(condition5).ToList();

                OrderModel2 orderModel5 = new OrderModel2();
                int a5 = query5.Count();
                double b5 = allCount.Count();

                orderModel5.value = a5;
                orderModel5.name = "18至25元" + "     " + (a5 / b5).ToString("0.00%");
                dataList2.Add(orderModel5);

                //4. 25 元
                condition6 = x => x.orderStatus == 1 && x.orderSumprice + x.discountMoney > 25 ;
                var query6 = bll.Search(condition6).ToList();

                OrderModel2 orderModel6 = new OrderModel2();
                int a6 = query6.Count();
                double b6 = allCount.Count();

                orderModel6.value = a6;
                orderModel6.name = "25元以上" + "     " + (a6 / b6).ToString("0.00%");
                dataList2.Add(orderModel6);
            }

            // 根据店铺查询  所有的时间段的

            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) && storeid != null && storeid != "")
            {
                int storeID = Convert.ToInt32(storeid);
                // 1.
                var allCount = bll.Search(x => x.orderStatus == 1 && x.storeid == storeID).ToList();//该店 所有的订单数量

                //2.  1 元一下
                condition1 = x => x.orderStatus == 1 && x.orderSumprice + x.discountMoney <= 1 && x.storeid == storeID;
                var query1 = bll.Search(condition1).ToList();
                OrderModel2 orderModel1 = new OrderModel2();
                int a = query1.Count();
                double b = allCount.Count();

                //orderModel1.value = a;//(a / b).ToString("0.00%");
                orderModel1.name = "1元以下" + "     " + (a / b).ToString("0.00%");
                dataList2.Add(orderModel1);

                //3.  1--6元
                condition2 = x => x.orderStatus == 1 && x.orderSumprice + x.discountMoney > 1 && x.orderSumprice + x.discountMoney <= 6 && x.storeid == storeID;
                var query2 = bll.Search(condition2).ToList();
                OrderModel2 orderModel2 = new OrderModel2();
                int a2 = query2.Count();
                double b2 = allCount.Count();
                orderModel2.value = a2;//(a / b).ToString("0.00%");
                orderModel2.name = "1至6元" + "     " + (a2 / b2).ToString("0.00%");
                dataList2.Add(orderModel2);

                //3.  6-12 元
                condition3 = x => x.orderStatus == 1 && x.orderSumprice + x.discountMoney > 6 && x.orderSumprice + x.discountMoney <= 12 && x.storeid == storeID;
                var query3 = bll.Search(condition3).ToList();

                OrderModel2 orderModel3 = new OrderModel2();
                int a3 = query3.Count();
                double b3 = allCount.Count();

                orderModel3.value = a3;
                orderModel3.name = "6至12元" + "     " + (a3 / b3).ToString("0.00%");
                dataList2.Add(orderModel3);

                //4.  12-18 元
                condition4 = x => x.orderStatus == 1 && x.orderSumprice + x.discountMoney > 12 && x.orderSumprice + x.discountMoney <= 18 && x.storeid == storeID;
                var query4 = bll.Search(condition4).ToList();

                OrderModel2 orderModel4 = new OrderModel2();
                int a4 = query4.Count();
                double b4 = allCount.Count();

                orderModel4.value = a4;
                orderModel4.name = "12至18元" + "     " + (a4 / b4).ToString("0.00%");
                dataList2.Add(orderModel4);

                //4.  18-25 元
                condition5 = x => x.orderStatus == 1 && x.orderSumprice + x.discountMoney > 18 && x.orderSumprice + x.discountMoney <= 25 && x.storeid == storeID;
                var query5 = bll.Search(condition5).ToList();

                OrderModel2 orderModel5 = new OrderModel2();
                int a5 = query5.Count();
                double b5 = allCount.Count();

                orderModel5.value = a5;
                orderModel5.name = "18至25元" + "     " + (a5 / b5).ToString("0.00%");
                dataList2.Add(orderModel5);

                //4. 25 元
                condition6 = x => x.orderStatus == 1 && x.orderSumprice + x.discountMoney > 25 && x.storeid == storeID;
                var query6 = bll.Search(condition6).ToList();

                OrderModel2 orderModel6 = new OrderModel2();
                int a6 = query6.Count();
                double b6 = allCount.Count();

                orderModel6.value = a6;
                orderModel6.name = "25元以上" + "     " + (a6 / b6).ToString("0.00%");
                dataList2.Add(orderModel6);
            }

            //DateTime datetime1 = DateTime.Parse(date1);
            //DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
            //condition = x => x.orderStatus == 1 && x.orderTime >= datetime1 && x.orderTime <= datetime2;

            // 根据时间查询  总的
            if (date1 != null && date2 != null && date1 != "" && date2 != ""&& string.IsNullOrEmpty(storeid))
            {
                // 1.
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                var allCount = bll.Search(x => x.orderStatus == 1 && x.orderTime >= datetime1 && x.orderTime <= datetime2).ToList();//该店 所有的订单数量

                //2.  1 元一下

                condition1 = x => x.orderStatus == 1 && x.orderSumprice + x.discountMoney <= 1 && x.orderTime >= datetime1 && x.orderTime <= datetime2;
                var query1 = bll.Search(condition1).ToList();
                OrderModel2 orderModel1 = new OrderModel2();
                int a = query1.Count();
                double b = allCount.Count();

                //orderModel1.value = a;//(a / b).ToString("0.00%");
                orderModel1.name = "1元以下" + "     " + (a / b).ToString("0.00%");
                dataList2.Add(orderModel1);

                //3.  1--6元
                condition2 = x => x.orderStatus == 1 && x.orderSumprice + x.discountMoney > 1 && x.orderSumprice + x.discountMoney <= 6 && x.orderTime >= datetime1 && x.orderTime <= datetime2;
                var query2 = bll.Search(condition2).ToList();
                OrderModel2 orderModel2 = new OrderModel2();
                int a2 = query2.Count();
                double b2 = allCount.Count();
                orderModel2.value = a2;//(a / b).ToString("0.00%");
                orderModel2.name = "1至6元" + "     " + (a2 / b2).ToString("0.00%");
                dataList2.Add(orderModel2);

                //3.  6-12 元
                condition3 = x => x.orderStatus == 1 && x.orderSumprice + x.discountMoney > 6 && x.orderSumprice + x.discountMoney <= 12 && x.orderTime >= datetime1 && x.orderTime <= datetime2; 
                var query3 = bll.Search(condition3).ToList();

                OrderModel2 orderModel3 = new OrderModel2();
                int a3 = query3.Count();
                double b3 = allCount.Count();

                orderModel3.value = a3;
                orderModel3.name = "6至12元" + "     " + (a3 / b3).ToString("0.00%");
                dataList2.Add(orderModel3);

                //4.  12-18 元
                condition4 = x => x.orderStatus == 1 && x.orderSumprice + x.discountMoney > 12 && x.orderSumprice + x.discountMoney <= 18 && x.orderTime >= datetime1 && x.orderTime <= datetime2;
                var query4 = bll.Search(condition4).ToList();

                OrderModel2 orderModel4 = new OrderModel2();
                int a4 = query4.Count();
                double b4 = allCount.Count();

                orderModel4.value = a4;
                orderModel4.name = "12至18元" + "     " + (a4 / b4).ToString("0.00%");
                dataList2.Add(orderModel4);

                //4.  18-25 元
                condition5 = x => x.orderStatus == 1 && x.orderSumprice + x.discountMoney > 18 && x.orderSumprice + x.discountMoney <= 25 && x.orderTime >= datetime1 && x.orderTime <= datetime2;
                var query5 = bll.Search(condition5).ToList();

                OrderModel2 orderModel5 = new OrderModel2();
                int a5 = query5.Count();
                double b5 = allCount.Count();

                orderModel5.value = a5;
                orderModel5.name = "18至25元" + "     " + (a5 / b5).ToString("0.00%");
                dataList2.Add(orderModel5);

                //4. 25 元
                condition6 = x => x.orderStatus == 1 && x.orderSumprice + x.discountMoney > 25 && x.orderTime >= datetime1 && x.orderTime <= datetime2;
                var query6 = bll.Search(condition6).ToList();

                OrderModel2 orderModel6 = new OrderModel2();
                int a6 = query6.Count();
                double b6 = allCount.Count();

                orderModel6.value = a6;
                orderModel6.name = "25元以上" + "     " + (a6 / b6).ToString("0.00%");
                dataList2.Add(orderModel6);
            }

            // 根据时间查询  和店铺查询

            if (date1 != null && date2 != null && date1 != "" && date2 != "" && storeid != "" && storeid!=null)
            {
                // 1.
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                int storeID = Convert.ToInt32(storeid);
                var allCount = bll.Search(x => x.orderStatus == 1 && x.orderTime >= datetime1 && x.orderTime <= datetime2 && x.storeid == storeID).ToList();//该店 所有的订单数量

                //2.  1 元一下

                condition1 = x => x.orderStatus == 1 && x.orderSumprice + x.discountMoney <= 1 && x.orderTime >= datetime1 && x.orderTime <= datetime2&&x.storeid==storeID;
                var query1 = bll.Search(condition1).ToList();
                OrderModel2 orderModel1 = new OrderModel2();
                int a = query1.Count();
                double b = allCount.Count();

                //orderModel1.value = a;//(a / b).ToString("0.00%");
                orderModel1.name = "1元以下" + "     " + (a / b).ToString("0.00%");
                dataList2.Add(orderModel1);

                //3.  1--6元
                condition2 = x => x.orderStatus == 1 && x.orderSumprice + x.discountMoney > 1 && x.orderSumprice + x.discountMoney <= 6 && x.orderTime >= datetime1 && x.orderTime <= datetime2 && x.storeid == storeID;
                var query2 = bll.Search(condition2).ToList();
                OrderModel2 orderModel2 = new OrderModel2();
                int a2 = query2.Count();
                double b2 = allCount.Count();
                orderModel2.value = a2;//(a / b).ToString("0.00%");
                orderModel2.name = "1至6元" + "     " + (a2 / b2).ToString("0.00%");
                dataList2.Add(orderModel2);

                //3.  6-12 元
                condition3 = x => x.orderStatus == 1 && x.orderSumprice + x.discountMoney > 6 && x.orderSumprice + x.discountMoney <= 12 && x.orderTime >= datetime1 && x.orderTime <= datetime2 && x.storeid == storeID;
                var query3 = bll.Search(condition3).ToList();

                OrderModel2 orderModel3 = new OrderModel2();
                int a3 = query3.Count();
                double b3 = allCount.Count();

                orderModel3.value = a3;
                orderModel3.name = "6至12元" + "     " + (a3 / b3).ToString("0.00%");
                dataList2.Add(orderModel3);

                //4.  12-18 元
                condition4 = x => x.orderStatus == 1 && x.orderSumprice + x.discountMoney > 12 && x.orderSumprice + x.discountMoney <= 18 && x.orderTime >= datetime1 && x.orderTime <= datetime2 && x.storeid == storeID;
                var query4 = bll.Search(condition4).ToList();

                OrderModel2 orderModel4 = new OrderModel2();
                int a4 = query4.Count();
                double b4 = allCount.Count();

                orderModel4.value = a4;
                orderModel4.name = "12至18元" + "     " + (a4 / b4).ToString("0.00%");
                dataList2.Add(orderModel4);

                //4.  18-25 元
                condition5 = x => x.orderStatus == 1 && x.orderSumprice + x.discountMoney > 18 && x.orderSumprice + x.discountMoney <= 25 && x.orderTime >= datetime1 && x.orderTime <= datetime2 && x.storeid == storeID;
                var query5 = bll.Search(condition5).ToList();

                OrderModel2 orderModel5 = new OrderModel2();
                int a5 = query5.Count();
                double b5 = allCount.Count();

                orderModel5.value = a5;
                orderModel5.name = "18至25元" + "     " + (a5 / b5).ToString("0.00%");
                dataList2.Add(orderModel5);

                //4. 25 元
                condition6 = x => x.orderStatus == 1 && x.orderSumprice + x.discountMoney > 25 && x.orderTime >= datetime1 && x.orderTime <= datetime2 && x.storeid == storeID;
                var query6 = bll.Search(condition6).ToList();

                OrderModel2 orderModel6 = new OrderModel2();
                int a6 = query6.Count();
                double b6 = allCount.Count();

                orderModel6.value = a6;
                orderModel6.name = "25元以上" + "     " + (a6 / b6).ToString("0.00%");
                dataList2.Add(orderModel6);
            }
            return Json(dataList2, JsonRequestBehavior.AllowGet);
        }


    }
}
